const peopleNumber = document.querySelector('.title>b');
const INIT_PEOPLE_COUNT = 1;
const CHANGE_PEOPLE_COUNT = 1;
const CLOSE = 2;

const worker = new Worker('/js/getPeopleNum.js');
function getAnswerCount(){
    return axios.get('/answer/count').then(({data})=>{
        if(data.code){
            return data.data;
        }
        alert(res.data.msg);
    });
}

function renderAnswerCount(num){
    peopleNumber.innerHTML = num;
}

async function setPeople(){
    const {answerCount} = await getAnswerCount();
    renderAnswerCount(answerCount);
    worker.postMessage(JSON.stringify({
        code:INIT_PEOPLE_COUNT,
        answerCount
    }))
}

setPeople();

worker.onmessage = function({data}) {
    switch(data.code){
        case CHANGE_PEOPLE_COUNT:
            console.log(data);
            renderAnswerCount(data.answerCount)
        break;
        case CLOSE:  // 结束线程
            worker.terminate();
        break;
    }
}

// 监听错误
worker.addEventListener('error',function(error){
    console.log(error);
    worker.terminate();
})